using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Diagnostics;

namespace CSP.WebParts
{
    [Guid("2e89ed15-1773-4014-a786-826e4e57fad3")]
    public class CSPMyPeepsLink : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;
        private string _myProperty = null;
        LinkButton linkBtnMyPeep;

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [System.ComponentModel.Category("My Property Group")]
        [WebDisplayName("MyProperty")]
        [WebDescription("Meaningless Property")]
        public string MyProperty
        {
            get
            {
                if (_myProperty == null)
                {
                    _myProperty = "Hello SharePoint My Peep";
                }
                return _myProperty;
            }
            set { _myProperty = value; }
        }


        public CSPMyPeepsLink()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {

                    base.CreateChildControls();

                    // Your code here...
                    //this.Controls.Add(new LiteralControl(this.MyProperty));
                    //RMV Initialized the My Peeps Control
                    InitControls();
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        private void InitControls()
        {
            //Create button
            InitURL();            
            linkBtnMyPeep = new LinkButton(); 
            //linkBtnMyPeep.CssClass = 
            linkBtnMyPeep.Enabled = true;
            
            
        }

        private void InitURL()
        {
            string currentLoggedUser = string.Empty;

            SPSite siteColl = null;
            SPWeb site = null;

            siteColl = SPContext.Current.Site;
            site = SPContext.Current.Web;

            try
            {
                SPUser currentuser = site.CurrentUser;
                if (currentuser != null)
                    currentLoggedUser = currentuser.LoginName;

               //Set MyPeeps URL
                string urlString = site.Url;
                Debug.WriteLine("URL String = {0}", urlString);

                string myPeepsPage = "/personal/MyPeeps.aspx";
                string pageurl = urlString + myPeepsPage;

            }
            catch (Exception exp)
            {

                Debug.WriteLine(exp.ToString());
                HandleException(exp);

            }   
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
